$(function() {
	module('basic');
	test("Global Wikid Object", 1, function() {
		ok(wikid != null, "Wikid exists in global namespace");
	});
	test("Parse DB name", 1, function() {
		var url = "http://localhost:5984/_wikid/?Person";
		var result = wikid.parseDBname(url);
		equals(result, "wikid", "Matched DB name");
	});
	
	
	test("Parse topic name, simple", 1, function() {
		var url = "http://localhost:5984/_wikid/?Person";
		var result = wikid.parseTopicName(url);
		equals(result, "Person",  "Matched DB name");
	});
	test("Parse topic name, param", 1, function() {
		var url = "http://localhost:5984/_wikid/?name=Person";
		var result = wikid.parseTopicName(url);
		equals(result, "Person", "Matched DB name");
	});
	test("Parse topic name, with anchor, param", 1, function() {
		var url = "http://localhost:5984/_wikid/?Person#anchor";
		var result = wikid.parseTopicName(url);
		equals(result, "Person", "Matched DB name");
	});



	test("Parse topic name, param", 1, function() {
		var url = "http://localhost:5984/_wikid/?name=Person";
		var result = wikid.parseTopicName(url);
		equals( result, "Person", "Matched DB name");
	});
	test("Parse topic name, with other multiple params", 1, function() {
		var url = "http://localhost:5984/_wikid/?a=b&c=d&name=Person&e=f";
		var result = wikid.parseTopicName(url);
		equals(result, "Person", "Matched DB name");
	});
	test ("Human Readable String from ID", 1, function() {
		var id = "Person_Long_Name";
		var result = wikid.titleFromTopicId(id);
		equals(result, "Person Long Name", "Readable ID");
	});
	
	test ("Parse Tags from Url", 2, function() {
		var url = "http://localhost:5984/_wikid/edit?tags=Person";
		var result = wikid.parseTagsToAdd(url);
		equals( result.length, 1, "One tag found");
		equals( result[0], "Person", "Matched the tag");
	});
	test ("Parse Multiple Tags from Url", 3, function() {
		var url = "http://localhost:5984/_wikid/edit?tags=Person,Contact";
		var result = wikid.parseTagsToAdd(url);
		equals( result.length, 2, "Two tags found");
		equals( result[0], "Person",  "Matched first the tag");
		equals( result[1], "Contact", "Matched second the tag");
	});	
	
	test ("Parse with no tags", 1, function() {
		var url = "http://localhost:5984/_wikid/edit?new=Project";
		var result = wikid.parseTagsToAdd(url);
		equals( result.length, 1, "No tags found");
	});		
	
	
	test("Parse topic name, with other params", 1, function() {
		var url = "http://localhost:5984/_wikid/?Person&tags=Project";
		var result = wikid.parseTopicName(url);
		equals(result, "Person",  "Matched DB name");
	});	
	
	
	test ("Generate wikid object",17, function() {
		var doc = {
		  _id : "Test_Object",
		  '-tags' : ["A_B", "B"],
		  '-wiki' : "A test wiki",
		  '-instance' : {
			 fields : {},
			 addmenu : {Story: {tags:["Agile_Story", {jsonPath:"$.doc._id"}]} },
			 toolsmenu : {}
		  },
		  '-path' : ['A', 'B', 'C'],
		  'A_B' : "Some",
		  "B" : ["Stuff"],
		  "Other" : {"name" : "value"},
		   "_attachments": {
			   "gb-logo-trim.psd": {
				   "content_type": "application/octet-stream",
				   "revpos": 9,
				   "length": 91873,
				   "stub": true
			   },
			   "gb-logo-trim.png": {
				   "content_type": "image/png",
				   "revpos": 7,
				   "length": 2659,
				   "stub": true
			   }
		   }
		};
		var db_mock = createViewMock(null);
		var result = wikid.toWikid(doc, db_mock); 
		ok(result.doc, "result has doc property");
		equals(result.topic, "Test Object");
		equals(result.title, "Test Object (Greenbridge)");
		equals(result.wiki, "<p>A test wiki</p>");
		equals(result.tags.length, 2, "Two tags");
		equals(result.tags[0].href, "wiki.html?A_B", "Correct href for a tag");
		equals(result.tags[0].alias, "A B", "Correct alias for a tag");
		equals(result.path.length, 3);
		equals(result.path[0].alias	, 'A');
		
		equals(result.fields.length, 3, "Three fields [Two from tags, one from other]");
		equals(result.fields[0].id, "A_B", "String field id");
		equals(result.fields[0].name, "A B", "String field name");
		equals(result.fields[0].value, "Some", "String field value");
		equals(result.fields[1].value[0], "Stuff", "Array field");
		equals(result.fields[2].value.name, "value", "Object field");
		
		equals(result.attachments.length, 2, "Two attachments");
		equals(result.attachments[0].name, "gb-logo-trim.psd", "Attachment Name");
	});
	
	test("Wikid Object lists children", 1, function() {
		// given a document of id of 'Test_Object'
		var doc = {
		  _id : "Test_Object",
		};
	
		// and two documents with ids Page2 and Page_3
		var result_rows = [];
		var child = {};
		child.value = "Page2";
		result_rows.push(child);
		var child2 = {};
		child2.value = "Page_3";
		result_rows.push(child2);
		var db_mock = createViewMock({"system/doc_by_tag" : result_rows});
		
		// when generating a wikid object
		var result = wikid.toWikid(doc, db_mock); 
		
		//then the wikid object will have a field tagged with 2 links
		equals(result.tagged.length, 2, "Two tags");
	});
	
	test("Recently Changed Topics", 3, function() {
		var result_rows = [
			{"id":"Change_This_On_Method_Call","key":"2010-05-12","value":"Change_This_On_Method_Call"},
			{"id":"Add_A_Menu_Item_From_Tag","key":"2010-05-11","value":"Add_A_Menu_Item_From_Tag"},
			{"id":"Test_Project_1","key":"2010-05-10","value":"Test_Project_1"},
			{"id":"Project","key":"2010-05-10","value":"Project"}];
		var db_mock = createViewMock({"system/changes_by_date" : result_rows });
		var result = wikid.findRecentChangesByDate(db_mock);
		equals(result.length, 3, "Three Dates");
		equals(result[0].date, "2010-05-12");
		equals(result[0].links[0].alias, "Change This On Method Call"); 
	});
	test("Generate Path Links", 3, function() {
		var links = ["A_B", "V"];
		var result = wikid.generatePathLinks(links);
		equals(result.length, 2);
		equals(result[0].alias, "A B");
		equals(result[1].href, "wiki.html?V");
	});
		
})

	function createViewMock(view_to_result_row) {
		var db_mock = {};
		db_mock.view = function(name, options) {
			var result = {};	
			result.rows = null;
			if (view_to_result_row && view_to_result_row[name]) {
				result.rows = view_to_result_row[name];
			}
			return result;
		}
		db_mock.info = function() {
			return {
				db_name : '/_wikid/scripts/test-modules/mockTags'
			};
		}
		return db_mock;
	}